<template>
  <el-container id="app">
    <el-header :height="`${height}px`" id="J_prismPlayer"></el-header>
    <div class="col-title container-fluid">
      <div class="row col-bg">
        <div class="col">
          <h5 v-text="title"></h5>
        </div>
        <div class="col text-right t525252">发布于：{{(new Date(updatedAt)).toLocaleDateString()}}</div>
      </div>
    </div>
    <el-main class="main">
      <div class="text-right text-18">视频列表</div>
      <el-container class="list" v-for="(v,i) in librays" :key="`${i}App1`">
        <el-aside width="165px" class="col-aside col1 rounded-top">
          <a class="d-block" :href="href(v.ID)">
            <el-image
              style="border-radius: 1rem;width:160px;height:90px;background:#000;"
              :src="v.CoverURL"
              fit="contain"
            ></el-image>
          </a>
        </el-aside>
        <el-main class="text-left col1">
          <div class="texth4" v-text="v.title"></div>
          <!-- <div class="t525252">发布于：{{(new Date(v.updatedAt)).toLocaleDateString()}}</div> -->
          <div class="t525252">发布于：{{v.updatedAt}}</div>
        </el-main>
      </el-container>
    </el-main>
    <div class="position-fixed position-fixed-end">
      <el-row>
        <el-col :span="18">
          <a class="d-block" href="https://h5.youzan.com/v3/im/index?c=wsc&v=2&kdt_id=42584494#/index">
            <el-image
              fit="contain"
              style="height:100%"
              src="https://cdn.hterobot.com/vue/library/image/onlinebutton.png"
            ></el-image>
          </a>
        </el-col>
        <el-col :span="6">
          <a
            class="d-block text-right"
            :href="purchase?purchase:'https://shop42776662.m.youzan.com/v2/showcase/homepage?kdt_id=42584494&ps=760'"
          >
            <el-image
              style="height:100%"
              fit="contain"
              src="https://cdn.hterobot.com/vue/library/image/buybutton.png"
            ></el-image>
          </a>
        </el-col>
      </el-row>
    </div>
  </el-container>
</template>

<script>
import {
  asyncgetcodingrobot,
  asyncgetcodingrobotlist
} from "../plugins/api.js";
export default {
  name: "Home",
  data() {
    return {
      activeName: "first",
      height: "300",
      PlayAuth: null,
      VideoId: null,
      CoverURL: null,
      title: null,
      info: null,
      updatedAt: null,
      purchase: null,
      videos: [],
      librays: [],
      user: {}
    };
  },
  async mounted() {
    this.height = (window.innerWidth * 9) / 16;
    this.start();
  },
  methods: {
    async start() {
      const id = this.GetQueryString("state");
      this.user.openid = this.GetQueryString("openid");
      this.user.username = this.GetQueryString("username");
      this.user.city = this.GetQueryString("city");
      this.user.province = this.GetQueryString("province");
      this.user.unionid = this.GetQueryString("unionid");
      this.user.sex = this.GetQueryString("sex");
      try {
        const { video, libray } = await asyncgetcodingrobot({ ID: id });
        const { title, updatedAt, info, purchase } = libray;
        const { PlayInfoList, VideoBase } = video;
        const { CoverURL, VideoId } = VideoBase;
        const { PlayInfo } = PlayInfoList;
        const source = PlayInfo[0].PlayURL;
        this.VideoId = VideoId;
        this.CoverURL = CoverURL;
        this.purchase = purchase;
        this.title = title;
        this.info = info;
        document.title = info;
        this.updatedAt = updatedAt;
        this.play(source, CoverURL, VideoId);
        let { videos, librays } = await asyncgetcodingrobotlist({ ID: id });
        librays = librays.filter(v=> v.info === libray.info)
        for (let v of librays) {
          if (videos && videos.VideoList) {
            let { CoverURL, Duration } = videos.VideoList.find(
              vo => vo.VideoId == v.videoid
            );
            v.CoverURL = CoverURL;
            v.Duration = Duration;
          }
        }
        this.librays = librays;
      } catch (err) {
        this.$router.push("/sess");
      }
    },
    GetQueryString(name) {
      const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      const r = window.location.search.substr(1).match(reg);
      if (r != null) {
        return r[2];
      } else {
        return "";
      }
    },
    play(source, CoverURL, VideoId) {
      this.player = new window.Aliplayer({
        id: "J_prismPlayer",
        autoplay: false,
        isLive: false,
        cover: CoverURL,
        rePlay: true,
        useH5Prism: true,
        useFlashPrism: false,
        x5_video_position: "normal",
        // x5_type: "h5",
        // playauth: PlayAuth,
        vid: VideoId,
        encryptType: 0,
        source,
        format: "mp4"
      });
    },
    href(ID) {
      const href = window.location.origin;
      const redirect_uri = encodeURIComponent(`${href}/wechat/codingrobot`);
      return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc38a242cea953176&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${ID}#wechat_redirect`;
    },
    Duration(value) {
      console.log(value);
      var secondTime = parseInt(value); // 秒
      var minuteTime = 0; // 分
      var hourTime = 0; // 小时
      if (secondTime > 60) {
        //如果秒数大于60，将秒数转换成整数
        //获取分钟，除以60取整数，得到整数分钟
        minuteTime = parseInt(secondTime / 60);
        //获取秒数，秒数取佘，得到整数秒数
        secondTime = parseInt(secondTime % 60);
        //如果分钟大于60，将分钟转换成小时
        if (minuteTime > 60) {
          //获取小时，获取分钟除以60，得到整数小时
          hourTime = parseInt(minuteTime / 60);
          //获取小时后取佘的分，获取分钟除以60取佘的分
          minuteTime = parseInt(minuteTime % 60);
        }
      }
      let 秒 = 0;
      let 分 = 0;
      let 时 = 0;
      if (parseInt(secondTime) < 9) {
        秒 = "0" + parseInt(secondTime);
      } else {
        秒 = "" + parseInt(secondTime);
      }

      if (minuteTime > 0) {
        分 = "" + parseInt(minuteTime);
      }
      if (hourTime > 0) {
        时 = parseInt(hourTime);
      }
      console.log(时);
      return `${分}:${秒}`;
    }
  }
};
</script>

<style scoped>
#app .el-header,
#app .el-main {
  padding: 0;
}
#app {
  background-color: #3e4c97;
}
#app .tex {
  margin-top: 15px;
  text-align: center;
}
.col1 {
  margin-left: 12px;
}
.texth4 {
  font-size: 1.1rem;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.col-aside {
  position: relative;
}
.col-timeser {
  color: #fff;
  width: 35px;
  height: 20px;
  line-height: 20px;
  font-size: 12px;
  position: absolute;
  right: 3px;
  bottom: 25px;
  text-align: center;
}
.col-title {
  margin-bottom: 10px;
}
.col-bg {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 5rem;
  background-color: #ffffff;
  border-bottom-right-radius: 1rem;
  border-bottom-left-radius: 1rem;
}
.col-bg div {
  font-size: 0.75rem;
}
.main {
  border-top-left-radius: 1rem;
  border-top-right-radius: 1rem;
  background-color: #ffffff;
  background-image: url(https://cdn.hterobot.com/vue/library/image/hte-IP.png);
  background-repeat: no-repeat;
  background-position-x: right;
  background-position-y: bottom;
}
.text-18 {
  font-size: 12px;
  line-height: 24px;
  margin-right: 16px;
  background-image: url(https://cdn.hterobot.com/vue/library/image/list2.png);
  background-repeat: no-repeat;
  background-position-y: 22px;
  background-position-x: 97%;
  background-size: 30px;
  margin-bottom: 12px;
}
.list {
  height: 140px;
  background-color: rgba(61, 76, 151, 0.12);
  margin: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 1rem;
  padding-left: 25px;
  padding-right: 25px;
}
.t525252 {
  color: #525252;
  font-size: 0.75rem;
}
.position-fixed-end {
  bottom: 0;
  width: 100%;
  height: 49px;
  background-color: #ffffff;
}
h5 {
  margin-bottom: 0px !important;
}
</style>
<style>
.prism-big-play-btn {
  margin-left: 30%;
  background-image: url(https://cdn.hterobot.com/vue/library/image/playbutton.png) !important;
}
</style>

